<?php

/**
* @file	s5_mp3_player.php - Modified from 1pixeloutplayer - duvien.com $
* @license GNU/GPL, see LICENSE.php, swf and js files are commercial and non-gpl licensed.
* Site: www.shape5.com
* Email: contact@shape5.com
* @copyright (C) 2009 Shape 5
*
*/

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.event.plugin');

$mainframe->addCustomHeadTag('<script src="'. JURI::base() .'plugins/content/s5_mp3_player/s5_mp3_player.js" type="text/javascript"></script>');
$mainframe->registerEvent( 'onPrepareContent', 's5_mp3_player' );
		
function s5_mp3_player( &$row, &$params, $page=0 ) {
	global $plugin;
	$regex = "#{s5_mp3(_+[a-zA-Z]+)?}(.*?){/s5_mp3(_+[a-zA-Z]+)?}#s"; 
	$plugin =& JPluginHelper::getPlugin('content', 's5_mp3_player');

	if ($plugin->published) {
		$row->text = preg_replace( $regex, '', $row->text );
		return;
	}
	// perform the replacement
	$row->text = preg_replace_callback( $regex, 's5_mp3_player_replacer', $row->text );
	return true;
}


function s5_mp3_player_parameterBuilder () {
	static $arParams = array();
	if (!$arParams) {
		// build the parameters list.
		$plugin =& JPluginHelper::getPlugin('content', 's5_mp3_player');
		$params = new JParameter( $plugin->params );
		//"autostart":"1",
		//"mp3path":"01 - They.mp3",
		//"playerID":"1"
		$arParamNames = array('allowDownload','repeat','color_mcPlayBtn','colorhover_mcPlayBtn',
		'color_mcPauseLabel','color_mcSeparationArrow1', 'color_mcSeparationArrow2',
		'color_mcPlayLabel','color_mcMiddleBg','color_mcMiddleBg2','color_mcMiddle-mcBorder',
		'color_mcMiddle-mcBg','color_mcMiddle-mcProgress','color_mcMiddle-mcSlider',
		'color_mcSoundControl-mcSoundBack','color_mcSoundControl-mcSoundSlider',
		'color_mcDownloadBtn','colorhover_mcDownloadBtn','textcolor_fmtTime',
		'textcolor_fmtSongName','textcolor_fmtDownload','text_txtDownload');

		foreach ($arParamNames as $sParamName) {
			if($sParamName == 'color_mcMiddleBg2' ){
				$arParams[] = "color_mcDownloadArrow=".$params->get($sParamName);
			}
			$arParams[] = $sParamName."=".$params->get($sParamName);
	
		}
		
	}
	static $playerWidth = '';
	static $playerHeight = '';
	if(!$playerWidth){
		$playerWidth = $params->get('width');
	}
	
	if(!$playerHeight){
		$playerHeight = $params->get('height');
	}
	
	static $sDefaultPath = '';
	if (!$sDefaultPath) {
		$sDefaultPath = JURI::base() . $params->get('defaultpath');  // zzz define the default path and hold it until needed
		$sDefaultPath = trim($sDefaultPath,' /'); // zzz
	}
	$arAllParams = array('sDefaultPath'=>$sDefaultPath,'playerWidth'=>$playerWidth,
	'playerHeight'=>$playerHeight,'arParams'=>$arParams);
	return $arAllParams;
}
function s5_mp3_player_replacer ( &$matches ) {

	$plugin =& JPluginHelper::getPlugin('content', 's5_mp3_player');
	$params = new JParameter( $plugin->params );

	$marginl = $params->get('marginl');
	$marginr = $params->get('marginr');
	$marginb = $params->get('marginb');
	$margint = $params->get('margint');

	$arAllParams = s5_mp3_player_parameterBuilder();
	extract ($arAllParams,EXTR_SKIP);

	global $ap_options, $ap_playerID;
	// Get next player ID
	$ap_playerID++;
	$arParams[] = "playerID=MP3Player_$ap_playerID";
	static $bAutostarted = 0;
	if (trim($matches[1])=='_auto') {
		// autostart is only engaged for the first mp3 on the page.
		if (!$bAutostarted) {
			$bAutostarted = 1;
			$arParams[] = 'autostart=1';
		}
	}
	$thisParams = explode("|",$matches[2]);
	if (strpos($thisParams[0],'http://')===FALSE and strpos($thisParams[0],'/')!==0) {
		// If the file is externally hosted or the path is redirected, do not use the default path
		$arParams[] = "mp3path={$sDefaultPath}/{$thisParams[0]}";
	} else {
		if (strpos($thisParams[0],'/')===0) {
			$arParams[] = "mp3path=".JURI::base().ltrim($thisParams[0],'/');
		} else {
			$arParams[] = "mp3path={$thisParams[0]}";
		}
	}

	$res = '<div style="padding:0px; margin-left:'.$marginl.'px; margin-right:'.$marginr.'px; margin-top:'.$margint.'px; margin-bottom:'.$marginb.'px" class="s5_mp3_player_wrap"><object type="application/x-shockwave-flash" data="'. JURI::base() .'plugins/content/s5_mp3_player/s5_mp3_player.swf" id="MP3Player_'.$ap_playerID.'" height="'.$playerHeight.'" width="'.$playerWidth.'">
		<param name="movie" value="'. JURI::base() .'plugins/content/s5_mp3_player/s5_mp3_player.swf" />
		<param name="FlashVars" value="'.implode('&amp;',$arParams).'" />
		<param name="quality" value="high" />
		<param name="menu" value="false" />
		<param name="wmode" value="opaque" />
		</object>
</div>';
	return $res;
}
?>